<?php

/**
 * This is the model class for table "organization".
 *
 * The followings are the available columns in table 'organization':
 * @property integer $ID
 * @property integer $PARENT_ID
 * @property integer $USER_CREATED
 * @property integer $USER_UPDATED
 * @property string $ORG_NAME
 * @property string $DESCRIPTION
 * @property integer $TIME_CREATED
 * @property integer $TIME_UPDATED
 *
 * The followings are the available model relations:
 * @property Employee[] $employees
 * @property Organization $pARENT
 * @property Organization[] $organizations
 */
class Organization extends CMasterModel {

    /**
     * Returns the static model of the specified AR class.
     * @return Organization the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'organization';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('PARENT_ID, USER_CREATED, USER_UPDATED', 'numerical', 'integerOnly' => true),
            array('ORG_NAME', 'length', 'max' => 150),
            array('DESCRIPTION', 'length', 'max' => 500),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('ID, PARENT_ID, USER_CREATED, USER_UPDATED, ORG_NAME, DESCRIPTION, TIME_CREATED, TIME_UPDATED', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'employees' => array(self::HAS_MANY, 'Employee', 'ID'),
            'pARENT' => array(self::BELONGS_TO, 'Organization', 'PARENT_ID'),
            'organizations' => array(self::HAS_MANY, 'Organization', 'PARENT_ID'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'ID' => 'ID',
            'PARENT_ID' => 'Parent',
            'USER_CREATED' => 'User Created',
            'USER_UPDATED' => 'User Updated',
            'ORG_NAME' => 'Org Name',
            'DESCRIPTION' => 'Description',
            'TIME_CREATED' => 'Time Created',
            'TIME_UPDATED' => 'Time Updated',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('ID', $this->ID);
        $criteria->compare('PARENT_ID', $this->PARENT_ID);
        $criteria->compare('USER_CREATED', $this->USER_CREATED);
        $criteria->compare('USER_UPDATED', $this->USER_UPDATED);
        $criteria->compare('ORG_NAME', $this->ORG_NAME, true);
        $criteria->compare('DESCRIPTION', $this->DESCRIPTION, true);
        $criteria->compare('TIME_CREATED', $this->TIME_CREATED);
        $criteria->compare('TIME_UPDATED', $this->TIME_UPDATED);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getOrgNameID() {
        //dkmprm,dkmnama
        return $this->ORG_NAME . '(' . $this->ID . ')';
    }


}